<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Locale Support</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../stream_converter.html" title="boost::cnv::stream Converter">
<link rel="prev" href="../stream_converter.html" title="boost::cnv::stream Converter">
<link rel="next" href="supported_string_types.html" title="Supported String Types">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav">
<a accesskey="p" href="../stream_converter.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../stream_converter.html"><img src="../../../images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="supported_string_types.html"><img src="../../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_convert.converters_detail.stream_converter.locale_support"></a><a class="link" href="locale_support.html" title="Locale Support">Locale
        Support</a>
</h4></div></div></div>
<p>
          Locale support is similar to the formatting support as demonstrated by
          the following example:
        </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">cnv</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">;</span>
<span class="keyword">namespace</span> <span class="identifier">arg</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">parameter</span><span class="special">;</span>
</pre>
<p>
        </p>
<p>
</p>
<pre class="programlisting">    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span> <span class="identifier">cnv</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span>  <span class="identifier">rus_locale</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span>  <span class="identifier">eng_locale</span><span class="special">;</span>

    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">eng_locale_name</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"English_United States.1251"</span> <span class="special">:</span> <span class="string">"en_US.UTF-8"</span><span class="special">;</span>
    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">rus_locale_name</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"Russian_Russia.1251"</span> <span class="special">:</span> <span class="string">"ru_RU.UTF-8"</span><span class="special">;</span>
    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">rus_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1,235e-002"</span>  <span class="special">:</span> <span class="string">"1,235e-02"</span><span class="special">;</span>
    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">eng_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1.235e-002"</span>  <span class="special">:</span> <span class="string">"1.235e-02"</span><span class="special">;</span>
    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">dbl_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1.2345E-002"</span> <span class="special">:</span> <span class="string">"1.2345E-02"</span><span class="special">;</span>

<span class="comment">//  cnv(std::setprecision(4))(std::uppercase)(std::scientific);</span>
    <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">precision</span> <span class="special">=</span> <span class="number">4</span><span class="special">)</span>
       <span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">uppercase</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">)</span>
       <span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">notation</span> <span class="special">=</span> <span class="identifier">cnv</span><span class="special">::</span><span class="identifier">notation</span><span class="special">::</span><span class="identifier">scientific</span><span class="special">);</span>

    <span class="keyword">double</span> <span class="identifier">double_v01</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</span><span class="identifier">dbl_expected</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
    <span class="identifier">string</span> <span class="identifier">double_s02</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">);</span>

    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">dbl_expected</span> <span class="special">==</span> <span class="identifier">double_s02</span><span class="special">);</span>

    <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">rus_locale</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span><span class="special">(</span><span class="identifier">rus_locale_name</span><span class="special">);</span> <span class="special">}</span>
    <span class="keyword">catch</span> <span class="special">(...)</span> <span class="special">{</span> <span class="identifier">printf</span><span class="special">(</span><span class="string">"Bad locale %s.\n"</span><span class="special">,</span> <span class="identifier">rus_locale_name</span><span class="special">);</span> <span class="identifier">exit</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="special">}</span>

    <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">eng_locale</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span><span class="special">(</span><span class="identifier">eng_locale_name</span><span class="special">);</span> <span class="special">}</span>
    <span class="keyword">catch</span> <span class="special">(...)</span> <span class="special">{</span> <span class="identifier">printf</span><span class="special">(</span><span class="string">"Bad locale %s.\n"</span><span class="special">,</span> <span class="identifier">eng_locale_name</span><span class="special">);</span> <span class="identifier">exit</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="special">}</span>

<span class="comment">//  cnv(std::setprecision(3))(std::nouppercase);</span>
    <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">precision</span> <span class="special">=</span> <span class="number">3</span><span class="special">)(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">uppercase</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span>

    <span class="identifier">string</span> <span class="identifier">double_rus</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">rus_locale</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad double_rus"</span><span class="special">);</span>
    <span class="identifier">string</span> <span class="identifier">double_eng</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">eng_locale</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad double_eng"</span><span class="special">);</span>

    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">double_rus</span> <span class="special">==</span> <span class="identifier">rus_expected</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">double_eng</span> <span class="special">==</span> <span class="identifier">eng_expected</span><span class="special">);</span>
</pre>
<p>
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2022 Vladimir
      Batov<p>
        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../stream_converter.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../stream_converter.html"><img src="../../../images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="supported_string_types.html"><img src="../../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
